#include "jy901s.h"
#include "stm32f4xx_hal.h"
#include "wit_c_sdk.h"
#include "REG.h"
#include "usart.h"
#include "delay.h"
volatile char s_cDataUpdate = 0, s_cCmd = 0xff;
void JY901S_Init(void)
{
    WitInit(WIT_PROTOCOL_NORMAL, 0x50);
    WitSerialWriteRegister(SensorUartSend);
	WitRegisterCallBack(SensorDataUpdata);
	WitDelayMsRegister(Delayms);

}
void JY901S_Read_ANGLE(JY901S_ANGLE *angle)
{
    s_cDataUpdate = 0;
	WitReadReg(Roll, 3);
    Delayms(100);
	// HAL_Delay(100);
    if (s_cDataUpdate & ANGLE_UPDATE)
    {
        angle->my_Roll = sReg[Roll] / 32768.0f * 180.0f;
        angle->my_Pitch = sReg[Pitch] / 32768.0f * 180.0f;
        angle->my_Yaw = sReg[Yaw] / 32768.0f * 180.0f;
        s_cDataUpdate &= ~ANGLE_UPDATE;
        printf("angle:%d %d %d\r\n", (int)angle->my_Roll, (int)angle->my_Pitch, (int)angle->my_Yaw);
    }
}
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize)
{
    //使用hal库串口2发送uiSize个字节的数据
    HAL_UART_Transmit(&huart2, p_data, uiSize, 0xffff);
}
static void Delayms(uint32_t ucMs)
{
    // for(int i = 0; i < ucMs*10000; i++)
    // {
        
    // }
     delay_us(ucMs*1000);

}
static void SensorDataUpdata(uint32_t uiReg, uint32_t uiRegNum)
{
    int i;
    for(i = 0; i < uiRegNum; i++)
    {
        switch(uiReg)
        {
//            case AX:
//            case AY:
            case AZ:
				s_cDataUpdate |= ACC_UPDATE;
            break;
//            case GX:
//            case GY:
            case GZ:
				s_cDataUpdate |= GYRO_UPDATE;
            break;
//            case HX:
//            case HY:
            case HZ:
				s_cDataUpdate |= MAG_UPDATE;
            break;
//            case Roll:
//            case Pitch:
            case Yaw:
				s_cDataUpdate |= ANGLE_UPDATE;
            break;
            default:
				s_cDataUpdate |= READ_UPDATE;
			break;
        }
		uiReg++;
    }

}
